# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(default_visibility = ["//visibility:public"])

licenses(["notice"])

cc_library(
    name = "id",
    hdrs = [
        "id_public_overrides/pw_thread_backend/id_inline.h",
        "id_public_overrides/pw_thread_backend/id_native.h",
    ],
    strip_include_prefix = "id_public_overrides",
    deps = [
        ":id_private",
    ],
)

cc_library(
    name = "id_private",
    hdrs = [
        "public/pw_thread_freertos/id_inline.h",
        "public/pw_thread_freertos/id_native.h",
    ],
    strip_include_prefix = "public",
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    visibility = ["//visibility:private"],
    deps = [
        "//pw_assert:assert",
        "//pw_interrupt:context",
        "//pw_thread:id.facade",
        "@freertos",
    ],
)

cc_library(
    name = "sleep",
    hdrs = [
        "sleep_public_overrides/pw_thread_backend/sleep_inline.h",
    ],
    strip_include_prefix = "sleep_public_overrides",
    deps = [
        ":sleep_private",
    ],
)

cc_library(
    name = "sleep_private",
    srcs = [
        "sleep.cc",
    ],
    hdrs = [
        "public/pw_thread_freertos/sleep_inline.h",
    ],
    implementation_deps = ["//pw_assert:check"],
    strip_include_prefix = "public",
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    visibility = ["//visibility:private"],
    deps = [
        "//pw_chrono:system_clock",
        "//pw_thread:sleep.facade",
        "//pw_thread:thread",
        "@freertos",
    ],
)

cc_library(
    name = "thread",
    srcs = [
        "thread.cc",
    ],
    hdrs = [
        "thread_public_overrides/pw_thread_backend/thread_inline.h",
        "thread_public_overrides/pw_thread_backend/thread_native.h",
    ],
    implementation_deps = ["//pw_assert:check"],
    strip_include_prefix = "thread_public_overrides",
    deps = [
        ":freertos_tasktcb",
        ":thread_private",
        "//pw_preprocessor",
        "//pw_thread:thread.facade",
        "@freertos",
    ],
)

cc_library(
    name = "thread_private",
    hdrs = [
        "public/pw_thread_freertos/config.h",
        "public/pw_thread_freertos/context.h",
        "public/pw_thread_freertos/options.h",
        "public/pw_thread_freertos/thread_inline.h",
        "public/pw_thread_freertos/thread_native.h",
    ],
    implementation_deps = [
        "//pw_assert:check",
        "//pw_string",
        # Include standard implementations of FreeRTOS application functions.
        "//third_party/freertos:support",
    ],
    strip_include_prefix = "public",
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    visibility = ["//visibility:private"],
    deps = [
        ":config_override",
        ":id",
        "//pw_assert:assert",
        "//pw_function",
        "//pw_span",
        "//pw_sync:binary_semaphore",
        "//pw_toolchain:constexpr_tag",
    ],
)

cc_library(
    name = "creation",
    hdrs = [
        "thread_creation_public_overrides/pw_thread_backend/context.h",
        "thread_creation_public_overrides/pw_thread_backend/options.h",
        "thread_creation_public_overrides/pw_thread_backend/priority.h",
        "thread_creation_public_overrides/pw_thread_backend/stack.h",
    ],
    strip_include_prefix = "thread_creation_public_overrides",
    deps = ["@freertos"],
)

label_flag(
    name = "config_override",
    build_setting_default = "//pw_build:default_module_config",
)

cc_library(
    name = "dynamic_test_threads",
    srcs = [
        "dynamic_test_threads.cc",
    ],
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    deps = [
        "//pw_chrono:system_clock",
        "//pw_thread:non_portable_test_thread_options",
        "//pw_thread:sleep",
        "//pw_thread:thread.facade",
    ],
    alwayslink = 1,
)

pw_cc_test(
    name = "dynamic_thread_backend_test",
    # TODO: https://pwbug.dev/271465588 - This test fails on-device.
    tags = ["do_not_run_test"],
    deps = [
        ":dynamic_test_threads",
        "//pw_thread:thread_facade_test",
    ],
)

cc_library(
    name = "static_test_threads",
    srcs = [
        "static_test_threads.cc",
    ],
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    deps = [
        "//pw_chrono:system_clock",
        "//pw_thread:non_portable_test_thread_options",
        "//pw_thread:sleep",
        "//pw_thread:thread.facade",
    ],
    alwayslink = 1,
)

pw_cc_test(
    name = "static_thread_backend_test",
    deps = [
        ":static_test_threads",
        "//pw_thread:thread_facade_test",
    ],
)

cc_library(
    name = "yield",
    hdrs = [
        "yield_public_overrides/pw_thread_backend/yield_inline.h",
    ],
    strip_include_prefix = "yield_public_overrides",
    deps = [
        ":yield_private",
    ],
)

cc_library(
    name = "yield_private",
    hdrs = [
        "public/pw_thread_freertos/yield_inline.h",
    ],
    strip_include_prefix = "public",
    visibility = ["//visibility:private"],
    deps = [
        "//pw_assert:assert",
        "//pw_thread:thread",
        "//pw_thread:yield.facade",
        "@freertos",
    ],
)

cc_library(
    name = "thread_iteration",
    srcs = [
        "pw_thread_freertos_private/thread_iteration.h",
        "thread_iteration.cc",
    ],
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    deps = [
        ":freertos_tasktcb",
        "//pw_function",
        "//pw_span",
        "//pw_status",
        "//pw_thread:thread_info",
        "//pw_thread:thread_iteration.facade",
        "//pw_thread_freertos:util",
    ],
)

pw_cc_test(
    name = "thread_iteration_test",
    srcs = [
        "pw_thread_freertos_private/thread_iteration.h",
        "thread_iteration_test.cc",
    ],
    # TODO: https://pwbug.dev/271465588 - This test fails on-device.
    tags = ["do_not_run_test"],
    deps = [
        ":freertos_tasktcb",
        ":static_test_threads",
        ":thread_iteration",
        "//pw_bytes",
        "//pw_span",
        "//pw_string:builder",
        "//pw_string:util",
        "//pw_sync:thread_notification",
        "//pw_thread:non_portable_test_thread_options",
        "//pw_thread:thread",
        "//pw_thread:thread_info",
        "//pw_thread:thread_iteration",
    ],
)

cc_library(
    name = "util",
    srcs = [
        "util.cc",
    ],
    hdrs = [
        "public/pw_thread_freertos/util.h",
    ],
    strip_include_prefix = "public",
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    deps = [
        "//pw_function",
        "//pw_log",
        "//pw_span",
        "//pw_status",
        "@freertos",
    ],
)

cc_library(
    name = "snapshot",
    srcs = [
        "snapshot.cc",
    ],
    hdrs = [
        "public/pw_thread_freertos/snapshot.h",
    ],
    # TODO: b/269204725 - Put this in the toolchain configuration instead.  I
    # would like to say `copts = ["-Wno-c++20-designator"]`, but arm-gcc tells
    # me that's an "unrecognized command line option"; I think it may be a
    # clang-only flag.
    copts = ["-Wno-pedantic"],
    strip_include_prefix = "public",
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    deps = [
        ":freertos_tasktcb",
        ":util",
        "//pw_function",
        "//pw_log",
        "//pw_protobuf",
        "//pw_status",
        "//pw_thread:snapshot",
        "//pw_thread:thread_pwpb",
    ],
)

cc_library(
    name = "freertos_tasktcb",
    hdrs = [
        "public/pw_thread_freertos/freertos_tsktcb.h",
    ],
    strip_include_prefix = "public",
    deps = [
        ":freertos_tasktcb_private",
        "@freertos",
    ],
)

cc_library(
    name = "freertos_tasktcb_private",
    hdrs = [":generate_freertos_tsktcb"],
    strip_include_prefix = "thread_public_overrides",
    visibility = ["//visibility:private"],
    deps = [
        "@freertos",
    ],
)

run_binary(
    name = "generate_freertos_tsktcb",
    srcs = [
        "@freertos//:tasks.c",
    ],
    outs = [":thread_public_overrides/pw_thread_freertos_backend/freertos_tsktcb.h"],
    args = [
        "--freertos-tasks-c=$(execpath @freertos//:tasks.c)",
        "--output=$(execpath :thread_public_overrides/pw_thread_freertos_backend/freertos_tsktcb.h)",
    ],
    tool = "//pw_thread_freertos/py:generate_freertos_tsktcb",
)

cc_library(
    name = "test_thread_context",
    hdrs = [
        "test_thread_context_public_overrides/pw_thread_backend/test_thread_context_native.h",
    ],
    strip_include_prefix = "test_thread_context_public_overrides",
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    deps = [
        ":test_thread_context_private",
    ],
)

cc_library(
    name = "test_thread_context_private",
    hdrs = [
        "public/pw_thread_freertos/test_thread_context_native.h",
    ],
    strip_include_prefix = "public",
    target_compatible_with = [
        "//pw_build/constraints/rtos:freertos",
    ],
    visibility = ["//visibility:private"],
    deps = [
        ":thread",
        "//pw_thread:test_thread_context.facade",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "docs.rst",
    ],
    prefix = "pw_thread_freertos/",
    target_compatible_with = incompatible_with_mcu(),
)
